﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BskLeague.Models;

namespace BskLeague.Models
{
    public class ScheduleGenerator
    {
        public int matchesNeeded { get; set; }
        public virtual List<Team> Teams { get; set; }
        public virtual List<Match> Matches { get; set; }
        public int MatchDuration { get; set; }
        public List<AvaibleTerm> Terms { get; set; }
        public int seed { get; set; }
        public List<Match> GenerateSchedule()
        {
            
            List<int> TeamsId = new List<int>();
            Matches = new List<Match>();
            Terms=Terms.OrderBy(c => c.time).ToList();
            foreach (Team t in Teams)
            {
                TeamsId.Add(t.TeamId);
            }
            if (Teams.Count % 2 == 1)
            {
                TeamsId.Add(-1);
            }
            

            if(seed<TeamsId.Count-1){
                seed++;
            }else{
                seed=0;
            }



            int fixTeam = TeamsId[seed];
            TeamsId.RemoveAt(seed);

            int buff = 0;
            int buff2 = TeamsId[TeamsId.Count - 1];

            //jeden obrot listy o jeden
            for (int i = 0; i < TeamsId.Count; i++)
            {
                buff = TeamsId[i];
                TeamsId[i] = buff2;
                buff2 = buff;
            }

            TeamsId.Insert(seed, fixTeam);








            int k = 0;
            for (int j = 0; j < TeamsId.Count - 1; j++)
            {
                //utworzenie par
                for (int i = 0; i < TeamsId.Count / 2; i++)
                {
                    if (TeamsId[i] != -1 && TeamsId[TeamsId.Count - i - 1] != -1)
                    {

                        Match m = new Match();
                        m.Team1 = new Team();
                        m.Team2 = new Team();
                        m.Place = new Stadium();
                        m.Team1.TeamId = TeamsId[i];
                        m.Team2.TeamId = TeamsId[TeamsId.Count - i - 1];
                        m.Round = j + 1;
                   
                        //m.Season;///// actual seazon!!!!!
                        m.Place.stadiumId = Terms[k].stadiumId;
                        m.Date = Terms[k].time;
                        k++;
                       // Terms.RemoveAt(Terms.Count-1);
                        Matches.Add(m);
                        //Matches.Insert(Matches.Count-1, m);
                    }
                }
                

            }
            BskLeagueDB _db = new BskLeagueDB();
            foreach (var m in Matches)
            {
                m.Confirmed1 = false;
                m.Confirmed2 = false;
                m.Team1 = _db.Teams.FirstOrDefault(v => v.TeamId == m.Team1.TeamId);
                m.Team2 = _db.Teams.FirstOrDefault(v => v.TeamId == m.Team2.TeamId);
                m.Place = _db.Stadiums.FirstOrDefault(v => v.stadiumId == m.Place.stadiumId);
                m.Season= _db.Seasons.Single(s => s.StatusId < 3);
            }
            _db.Dispose();
            return Matches;
        }

        public void WriteToDb()
        {
            BskLeagueDB _db = new BskLeagueDB();
            foreach (var m in Matches)
            {
                m.Confirmed1 = false;
                m.Confirmed2 = false;
                _db.Matchs.Add(m);
                _db.SaveChanges();
            }

            _db.Dispose();


        }
        public ScheduleGenerator()
        {
            seed = -1;
            Teams=new List<Team>();
            Terms = new List<AvaibleTerm>();
        }


    }

    public class AvaibleTerm
    {
        public int stadiumId {get; set;}
        public string stadiumName { get; set; }
        public DateTime time { get; set; }
    }
}